package com.studyyoun.android.nonghe.push;

import android.app.*;
import android.app.ActivityManager.*;
import android.content.*;
import android.os.*;
import android.text.*;


import com.studyyoun.android.androidcommonbaselibrary.execut.ExecutorFunction;
import com.studyyoun.android.androidcommonbaselibrary.utils.FastJsonUtil;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.nonghe.home.activity.HomeActivity;
import com.studyyoun.android.nonghe.push.sql.PushReceiverSqlDBHelper;
import com.studyyoun.android.nonghe.welcom.App;

import java.util.*;

import cn.jpush.android.api.*;

/**
 * Created by androidlongs on 17/6/22.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

public class JPushReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(final Context context, Intent intent) {

        String action = intent.getAction();

        if (!TextUtils.isEmpty(action)) {
            if (TextUtils.equals("cn.jpush.android.intent.NOTIFICATION_RECEIVED", action)) {
                Bundle bundle = intent.getExtras();
                String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
                String content = bundle.getString(JPushInterface.EXTRA_ALERT);
                String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
                int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
                //富媒体通知推送下载的HTML的文件路径,用于展现WebView
                String fileHtml = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_PATH);

                //富媒体通知推送下载的图片资源的文件名,多个文件名用 “，” 分开。 与 “JPushInterface.EXTRA_RICHPUSH_HTML_PATH” 位于同一个路径
                String fileStr = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_RES);

                if (!TextUtils.isEmpty(fileStr)) {
                    String[] fileNames = fileStr.split(",");
                }


                //大文本通知样式中大文本的内容
                String bigText = bundle.getString(JPushInterface.EXTRA_BIG_TEXT);

                //大图片通知样式中大图片的路径/地址
                String bigPicPath = bundle.getString(JPushInterface.EXTRA_BIG_PIC_PATH);


                LogUtils.d("极光推送消息 通知栏信息 " + title + "    " + content + " " + notificationId);

                JPushReceiverModel model = new JPushReceiverModel();
                if (!TextUtils.isEmpty(extras)) {
                    try {
                        model = FastJsonUtil.toBean(extras, JPushReceiverModel.class);
                        LogUtils.d("reciver msg " + model);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                model.msgTitle = title;
                model.msgContent = content;
                model.msgId = String.valueOf(notificationId);

                final JPushReceiverModel finalModel = model;
                ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
                    @Override
                    public void run() {
                        PushReceiverSqlDBHelper dbHelper = new PushReceiverSqlDBHelper(context);
                        dbHelper.insertArt(finalModel);
                    }
                });

            }


        } else if (TextUtils.equals("cn.jpush.android.intent.NOTIFICATION_OPENED", action)) {
            LogUtils.d("用户打开自定义通知栏的");

            context.startActivity(new Intent(context, HomeActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

        } else if (TextUtils.equals("cn.jpush.android.intent.REGISTRATION", action)) {
            Bundle bundle = intent.getExtras();
            String title = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            LogUtils.d("SDK 向 JPush Server 注册所得到的注册 ID  " + title);
        } else if (TextUtils.equals("cn.jpush.android.intent.MESSAGE_RECEIVED", action)) {
            Bundle bundle = intent.getExtras();
            String title = bundle.getString(JPushInterface.EXTRA_TITLE);
            //保存服务器推送下来的消息内容
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);


            LogUtils.d("收到了自定义消息 Push  " + title + "  " + extras);
        } else if (TextUtils.equals("cn.jpush.android.intent.NOTIFICATION_RECEIVED", action)) {
            Bundle bundle = intent.getExtras();
            String title = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            LogUtils.d("SDK 向 JPush Server 注册所得到的注册 ID  " + title);
        }

    }


    /**
     * 判断某个界面是否在前台
     *
     * @param className 某个界面名称
     */
    private boolean isForeground(String className) {
        if (TextUtils.isEmpty(className)) {
            return false;
        }

        ActivityManager am = (ActivityManager) App.getInstance().getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName())) {
                return true;
            }
        }

        return false;
    }
}
